package cn.tedu.csmall.passport.service.impl;

import cn.tedu.csmall.passport.mapper.RoleMappper;
import cn.tedu.csmall.passport.pojo.vo.RoleListItemVO;
import cn.tedu.csmall.passport.repo.RoleRedisRepositoryIml;
import cn.tedu.csmall.passport.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    RoleMappper roleMappper;
    @Autowired
    RoleRedisRepositoryIml repository;

    @Override
    public List<RoleListItemVO> list() {
        log.debug("开始处理查询角色列表的业务, 无参数");

        //从redis缓存中读取数据
        return repository.list();
    }

    @Override
    public void rebuildCache() {
        log.debug("开始处理重建角色列表的业务, 无参数");
        repository.deleteAll();
        List<RoleListItemVO> list = roleMappper.list();
        Iterator<RoleListItemVO> iterator = list.iterator();
        while (iterator.hasNext()){
            RoleListItemVO item = iterator.next();
            if (item.getId()==1) {
                iterator.remove();
                break;
            }
        }
        repository.save(list);
    }
}
